1455. 检查单词是否为句中其他单词的前缀
为保证权益,题目请参考 1455. 检查单词是否为句中其他单词的前缀(From LeetCode).
解决方案1
Python
python
# 1455. 检查单词是否为句中其他单词的前缀
# https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/
class Solution:
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
for idx, word in enumerate(sentence.split(" ")):
if word.startswith(searchWord):
return idx + 1
return -1
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11